package com.bi.design.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bi.design.entity.Device;
import com.bi.design.entity.DeviceData;
import com.bi.design.entity.Monitor;
import com.bi.design.mapper.MonitorMapper;
import com.bi.design.pojo.BasePage;
import com.bi.design.query.DeviceDataPageQuery;
import com.bi.design.query.DevicePageQuery;
import com.bi.design.service.DeviceDataService;
import com.bi.design.service.DeviceService;
import com.bi.design.tools.ApiResult;
import com.bi.design.tools.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/device/")
public class DeviceController {

    private final DeviceService deviceService;
    private final MonitorMapper monitorMapper;
    private final DeviceDataService deviceDataService;

    @PostMapping("/add-or-edit")
    public ApiResult<String> addAndEdit(@Validated @RequestBody Device dto) {
        log.info("录入的数据=>{}", dto);
        return ApiResult.ok(deviceService.addAndEdit(dto));
    }


    @PostMapping("/page")
    public ApiResult<PageInfo<Device>> page(@RequestBody DevicePageQuery query) {
        PageInfo<Device> pageInfo = deviceService.page(
                new PageInfo<>(query.getPageCurr(), query.getPageSize()), Wrappers.lambdaQuery(Device.class)
                        .eq(StringUtils.isNotBlank(query.getDeviceType()), Device::getDeviceType, query.getDeviceType())
                        .eq(StringUtils.isNotBlank(query.getDeviceName()), Device::getDeviceName, query.getDeviceName())
                        .eq(StringUtils.isNotBlank(query.getDeviceCode()), Device::getDeviceCode, query.getDeviceCode())
                        .orderByDesc(Device::getCreateTime)
        );
        return ApiResult.ok(pageInfo);
    }

    @PostMapping("/monitor/page")
    public ApiResult<PageInfo<Monitor>> monitorPage(@RequestBody BasePage query) {
        PageInfo<Monitor> pageInfo = monitorMapper.selectPage(
                new PageInfo<>(query.getPageCurr(), query.getPageSize()), Wrappers.lambdaQuery(Monitor.class)
                        .orderByDesc(Monitor::getCreateTime)
        );
        return ApiResult.ok(pageInfo);
    }

    @PostMapping("/data-page-code/{deviceCode}")
    public ApiResult<PageInfo<DeviceData>> dataPageByDeviceCode(@PathVariable String deviceCode, @RequestBody DeviceDataPageQuery query) {
        PageInfo<DeviceData> pageInfo = deviceDataService.page(
                new PageInfo<>(query.getPageCurr(), query.getPageSize()), Wrappers.lambdaQuery(DeviceData.class)
                        .eq(StringUtils.isNotBlank(deviceCode), DeviceData::getDeviceCode, deviceCode)
                        .orderByDesc(DeviceData::getCreateTime)
        );
        return ApiResult.ok(pageInfo);
    }

    @PostMapping("/data-page-type/{deviceType}")
    public ApiResult<PageInfo<DeviceData>> dataPageByDeviceType(@PathVariable String deviceType, @RequestBody DeviceDataPageQuery query) {
        return ApiResult.ok(deviceDataService.getPageByDeviceTypeQuery(deviceType, query));
    }

    @DeleteMapping("/del/{deviceCode}")
    public ApiResult<String> delete(@PathVariable String deviceCode) {
        deviceService.removeById(deviceCode);
        return ApiResult.ok();
    }

    @GetMapping("/get/{deviceCode}")
    public ApiResult<Device> get(@PathVariable String deviceCode) {
        return ApiResult.ok(deviceService.getById(deviceCode));
    }
}
